const info = uni.getSystemInfoSync();
const isIOS = info.platform === 'ios';
const isAndroid = info.platform === 'android';
// #ifdef H5
// window.app = window.app || {};
// window.webkit = window.webkit || {};
// window.webkit.messageHandlers = window.webkit.messageHandlers || {};
// #endif
const actions = {
	//获取手机号
	GETPHONENUMBER: 'getPhoneNumber',
	//获取token
	GETTOKEN: 'h5GetToken',
	//获取图片
	CHOOSEIMAGE: 'chooseImage',
	//微信分享
	WXSHARE: 'share',
	//登录
	LOGIN: 'toLogin',
	//保存到相册
	SAVEPHOTOTOALBUM: 'savePhotoToAlbum',
	//告知app路由变化
	UPDATEROUTER: 'updateRouter',
	//获取地理位置
	GETLOCATION: 'getLocation',
	//分享海报
	SHAREPOSTER: 'sharePoster'
}

/*
	getUUID 获取UUID
*/
function getUUID(len, radix) {
	var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
	var uuid = [],
		i;
	radix = radix || chars.length;
	if (len) {
		for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
	} else {
		var r;
		uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
		uuid[14] = '4';
		for (i = 0; i < 36; i++) {
			if (!uuid[i]) {
				r = 0 | Math.random() * 16;
				uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
			}
		}
	}
	return uuid.join('');
}
// 获取回调函数名
function getFunName(key) {
	return key + '_' + getUUID(8);
}

function execute(action, params) {
	const result = new Promise(resolve => {
		window[params.callbackName] = function(res) {
			if (Object.prototype.toString.call(res) === "[object String]") {
				if (/^[\{\[]/.test(res)) {
					res = JSON.parse(res.replace(/[\r\n]/g, ""))
				}
			}
			resolve(res);
		}
	})
	if (isIOS) {
		if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers[action]) {
			window.webkit.messageHandlers[action].postMessage(params);
			return result;
		} else {
			return Promise.reject('没有找到' + action + '方法')
		}
	}
	
	
	if (isAndroid) {
		if (window.app && window.app[action]) {
			window.app[action](JSON.stringify(params));
			return result;
		} else {
			return Promise.reject('没有找到' + action + '方法')
		}
	}
	return Promise.reject('非App集成环境，不支持该操作')
}

export default {
	getPhoneNumber() {//获取手机号
		return execute(actions.GETPHONENUMBER, {
			callbackName: getFunName('getPhoneNumber')
		});
	},
	getToken() {//获取token
		return execute(actions.GETTOKEN, {
			callbackName: getFunName('getToken')
		});
	},
	chooseImage(params = {//选择图片
		count: 1,
		sourceType: ['album', 'camera']
	}) {
		const {
			count,
			sourceType
		} = params;
		return execute(actions.CHOOSEIMAGE, {
			count,
			sourceType,
			callbackName: getFunName('chooseImage')
		});
	},
	savePhotoToAlbum(saveImageUrl) {//保存到相册
		if (!saveImageUrl) {
			return Promise.reject('缺少参数！')
		}
		return execute(actions.SAVEPHOTOTOALBUM, {
			saveImageUrl,
			callbackName: getFunName('savePhotoToAlbum')
		});
	},
	sharePoster(saveImageUrl) {//分享海报
		if (!saveImageUrl) {
			return Promise.reject('缺少参数！')
		}
		return execute(actions.SHAREPOSTER, {
			saveImageUrl,
			callbackName: getFunName('sharePoster')
		});
	},
	login() {//登录
		return execute(actions.LOGIN, {
			callbackUrl: location.href,
			callbackName: getFunName('login')
		});
	},
	updateRouter(params) {
		return execute(actions.UPDATEROUTER, {
			...params,
			callbackName: getFunName('updateRouter')
		});
	},
	getLocation(params) {//获取地理位置
		return execute(actions.GETLOCATION, {
			...params,
			callbackName: getFunName('getLocation')
		});
	},
	wxShare(params) {//微信分享
		if (!params) {
			return Promise.reject('缺少参数！')
		}
		const {
			title,
			imageUrl,
			shareUrl
		} = params;
		if (!title) {
			return Promise.reject('缺少参数【' + title + '】！')
		}
		if (!imageUrl) {
			return Promise.reject('缺少参数【' + imageUrl + '】！')
		}
		if (!shareUrl) {
			return Promise.reject('缺少参数【' + shareUrl + '】！')
		}
		return execute(actions.WXSHARE, {
			title,
			imageUrl,
			shareUrl,
			callbackName: getFunName('wxShare')
		});
	}
}
